package com.happyscan138.makefun.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.happyscan138.makefun.BaseAdapter.myNotice;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.View.RoundImageView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cz.msebera.android.httpclient.Header;

import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/1/1.
 */

public class myFriendNoticeAdapter extends BaseAdapter{

    public myFriendNoticeAdapter(Context mContext,List<myNotice> data,String access_key ) {
        log.e("notice","notice_Test");
        this.data = data;
        this.mContext = mContext;
        this.access_key = access_key;
    }
    private String access_key;
    private Context mContext;
    private List<myNotice> data;
    private String FocusID;

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder_myNotice viewHolder;
        if(convertView==null){
            viewHolder=new ViewHolder_myNotice();
            convertView=View.inflate(mContext, R.layout.my_notice_listview_item,null);
            viewHolder.myNotive_head=(RoundImageView) convertView.findViewById(R.id.my_notice_head);
            viewHolder.myNotive_name=(TextView) convertView.findViewById(R.id.my_notice_name);
            viewHolder.CacelNotice=(Button)convertView.findViewById(R.id.input_notice);
     //     viewHolder.myNotive_text=(TextView) convertView.findViewById(R.id.my_notice_text);

            //将viewHolder与converView绑定
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder_myNotice) convertView.getTag();
        }
            myNotice bean=data.get(position);
            FocusID=bean.focus_id;
       //     new LoadImagesTask(viewHolder.myNotive_head).execute(bean.head);
        Picasso.with(mContext).load(bean.head).into(viewHolder.myNotive_head);
            Log.e("bean.head",bean.head);
            viewHolder.myNotive_name.setText(bean.name);
            viewHolder.CacelNotice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    client=new AsyncHttpClient();
                    params=new RequestParams();
                    params.put("access_key",access_key);
                    params.put("focus_id",FocusID);
                    log.e("parames",params.toString());
                    log.e("access_key",access_key);
                    RequestHandle handle=client.post(Urls.CancelNotice,params,new JsonHttpResponseHandler(){
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            super.onSuccess(statusCode, headers, response);
                            log.e("onSuccess",response.toString());
                            String jsonstr=response.toString();
                            try {
                                JSONObject jsonObject=new JSONObject(jsonstr);
                                String msg = jsonObject.getString("msg");
                                Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
                                String code=jsonObject.getString("code");
                                int code_int=Integer.valueOf(code).intValue();
                                if(code_int==1) {
                                    data.remove(position);
                                    notifyDataSetChanged();
                                   Toast.makeText(mContext,msg,Toast.LENGTH_SHORT);
                                }else {
                                    Toast.makeText(mContext,msg,Toast.LENGTH_SHORT);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                            super.onFailure(statusCode, headers, responseString, throwable);
                            log.e("onFailure",responseString);
                        }
                    });
                }
            });
    //   viewHolder.myNotive_text.setText(bean.text);
        return convertView;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
class ViewHolder_myNotice{
    public RoundImageView myNotive_head;
    public TextView myNotive_name;
    public Button CacelNotice;
//    public TextView myNotive_text;

}